#!/usr/bin/env python
import os
import yaml
import argparse
from cpp2py import find, conf_path, make_path


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--conf', default=conf_path, help='input the config file path')
    parser.add_argument('-c', '--compile', action='store_true', help='compile the cpp files')
    parser.add_argument('--install', action='store_true', help='install the .so to system root')
    parser.add_argument('--clean', action='store_true', help='clean the object temp files')
    parser.add_argument('--uninstall', action='store_true', help='uninstall the .so file')

    args = parser.parse_args()

    with open(args.conf, 'r') as f:
        cdict = yaml.load(f, yaml.FullLoader)
    
    incs_path = os.path.abspath(cdict['include'])
    srcs_path = os.path.abspath(cdict['source'])

    proj_name = find(srcs_path)
    cmd = "make -f %s PROJ_NAME=%s INCS_ROOT=%s SRCS_ROOT=%s" % (make_path, 
                                                                 proj_name, 
                                                                 incs_path, 
                                                                 srcs_path)
    if args.compile:
        os.system(cmd)
    
    if args.install:
        os.system(cmd + " install")
    
    if args.clean:
        os.system(cmd + ' clean')

    if args.uninstall:
        os.system(cmd + ' uninstall')
